import pandas as pd
import os
import seaborn as sns
import numpy as np
import math
import matplotlib.pyplot as plt
from dataanalyze.plot import plotbar
class DataAnalyzer:

    def __init__(self):
        #获取父目录
        root = os.path.dirname(os.path.dirname(__file__))
        dataset_root = os.path.join(root,'dataset')
        infocache_root = os.path.join(root,r'dataanalyze\infocache')
        imgroot = os.path.join(infocache_root,'imgs')
        self.root = root
        self.datasetroot = dataset_root
        self.infocache_root = infocache_root
        self.imgroot = imgroot

    def base_train_analysis(self):
        # print(self.infocache_root)
        #注意中文 gbk编码

        basetrain_data_root = self.datasetroot + '\\' + 'base_train_sum.csv'
        self.basetrain_df = pd.read_csv(basetrain_data_root, encoding='gbk')
        basetrain_df = self.basetrain_df
        print(self.basetrain_df.info())
        base_info_dir = self.infocache_root + '\\' + 'base_train_Total.txt'
        if not os.path.exists(base_info_dir):
            basetrain_info = str(self.basetrain_df.describe())
            with open(base_info_dir, 'w', encoding='utf-8') as f:
                f.write(basetrain_info)
        else:
            f = open(base_info_dir, 'r', encoding='utf-8')
            for l in f:
                print(l)
        flag_nan_cnt = len([each for each in basetrain_df['flag'] if pd.isna(each)])
        registertime_nan_cnt =  len([each for each in basetrain_df['注册时间'] if pd.isna(each)])
        registercapital_nan_cnt = len([each for each in basetrain_df['注册资本'] if pd.isna(each)])
        # sns.distplot(basetrain_df['注册资本'], hist=False, rug=True)
        sns.distplot(basetrain_df['控制人持股比例'], hist=False, rug=True)
        plt.show()

        industry_nan_cnt = len([each for each in basetrain_df['行业'] if pd.isna(each)])
        region_nan_cnt = len([each for each in basetrain_df['区域'] if pd.isna(each)])
        enterprisetype_nan_cnt = len([each for each in basetrain_df['企业类型'] if pd.isna(each)])
        manipulatortype_nan_cnt = len([each for each in basetrain_df['控制人类型'] if pd.isna(each)])
        manipulatortor_holdshares_ratio_nan_cnt = len([each for each in basetrain_df['控制人持股比例'] if pd.isna(each)])
        flag_cat_dict ,registertime_cat_dict= {},{}
        flag_cat_dict['NAN'] = flag_nan_cnt
        flag_cat_dict.update(dict(basetrain_df['flag'].value_counts()))
        print(flag_cat_dict)
        plotbar(list(flag_cat_dict.keys()),list(flag_cat_dict.values()),'flag of base_train','type','type count')
        # print()
        # print(basetrain_df['注册资本'].value_counts())
        # print(basetrain_df['行业'].value_counts())
        # print(basetrain_df['区域'].value_counts())
        # print(basetrain_df['企业类型'].value_counts())
        # print(basetrain_df['控制人类型'].value_counts())
        # print(basetrain_df['控制人持股比例'].value_counts())
    def base_verify1_analysis(self):
        baseverify1_data_root = self.datasetroot + '\\' + 'base_verify1.csv'
        self.base_verify1_df = pd.read_csv(baseverify1_data_root, encoding='gbk')
        base_verify1_df = self.base_verify1_df
        print(self.base_verify1_df.info())
        base_info_dir = self.infocache_root + '\\' + 'base_verify1_Total.txt'
        if not os.path.exists(base_info_dir):
            basetrain_info = str(self.base_verify1_df.describe())
            with open(base_info_dir, 'w', encoding='utf-8') as f:
                f.write(basetrain_info)
        else:
            f = open(base_info_dir, 'r', encoding='utf-8')
            for l in f:
                print(l)

        flag_nan_cnt = len([each for each in base_verify1_df['flag'] if pd.isna(each)])
        registertime_nan_cnt = len([each for each in base_verify1_df['注册时间'] if pd.isna(each)])
        registercapital_nan_cnt = len([each for each in base_verify1_df['注册资本'] if pd.isna(each)])
        industry_nan_cnt = len([each for each in base_verify1_df['行业'] if pd.isna(each)])
        region_nan_cnt = len([each for each in base_verify1_df['区域'] if pd.isna(each)])
        enterprisetype_nan_cnt = len([each for each in base_verify1_df['企业类型'] if pd.isna(each)])
        manipulatortype_nan_cnt = len([each for each in base_verify1_df['控制人类型'] if pd.isna(each)])
        manipulatortor_holdshares_ratio_nan_cnt = len([each for each in base_verify1_df['控制人持股比例'] if pd.isna(each)])
        flag_cat_dict, registertime_cat_dict = {}, {}
        flag_cat_dict['NAN'] = flag_nan_cnt
        flag_cat_dict.update(dict(base_verify1_df['flag'].value_counts()))
        print(flag_cat_dict)
        plotbar(list(flag_cat_dict.keys()), list(flag_cat_dict.values()), 'flag of base_verify1', 'type', 'type count')
    def knowledge_train_sum_analysis(self):
        knowledge_train_sum_dir = self.datasetroot+'\\'+'knowledge_train_sum.csv'
        self.knowledge_train_sum_df = pd.read_csv(knowledge_train_sum_dir,encoding='gbk')
        knowledge_train_sum_df = self.knowledge_train_sum_df
        # sns.histplot(knowledge_train_sum_df['ID'])
        sns.histplot(knowledge_train_sum_df['专利'])
        sns.histplot(knowledge_train_sum_df['商标'])
        sns.histplot(knowledge_train_sum_df['著作权'])
        plt.show()
        print(knowledge_train_sum_df.info())
    def money_information_verify1_analysis(self):
        money_information_verify1_dir = self.datasetroot+'\\'+'money_information_verify1.csv'
        self.money_information_verify1_df = pd.read_csv(money_information_verify1_dir,encoding='gbk')
        money_information_verify1_df = self.money_information_verify1_df
        print(money_information_verify1_df.info())
    def money_report_train_sum_analysis(self):
        money_report_train_sum_dir = self.datasetroot+'\\'+'money_report_train_sum.csv'
        self.money_report_train_sum_df = pd.read_csv(money_report_train_sum_dir,encoding='gbk')
        money_report_train_sum_df = self.money_report_train_sum_df
        for each in money_report_train_sum_df.dropna():

            sns.distplot(money_report_train_sum_df[each],hist=False)

            plt.show()

        print(money_report_train_sum_df.info())
    def paient_information_verify1_analysis(self):
        paient_information_verify1_dir = self.datasetroot+'\\'+'paient_information_verify1.csv'
        self.paient_information_verify1_df = pd.read_csv(paient_information_verify1_dir,encoding='gbk')
        paient_information_verify1_df = self.paient_information_verify1_df
        print(paient_information_verify1_df.info())
    def year_report_train_sum(self):
        year_report_train_sum_df_dir = self.datasetroot+'\\'+'year_report_train_sum.csv'
        self.year_report_train_sum_df = pd.read_csv(year_report_train_sum_df_dir,encoding='gbk')
        year_report_train_sum_df = self.year_report_train_sum_df
        for each in year_report_train_sum_df.dropna():
            sns.distplot(year_report_train_sum_df[each], hist=False)

            plt.show()
        print(year_report_train_sum_df.info())
    def year_report_verify1(self):
        year_report_verify1_dir = self.datasetroot+'\\'+'year_report_verify1.csv'
        self.year_report_verify1_df = pd.read_csv(year_report_verify1_dir,encoding='gbk')
        year_report_verify1_df = self.year_report_verify1_df
        for each in year_report_verify1_df.dropna():
            sns.distplot(year_report_verify1_df[each], hist=False)

            plt.show()
        print(year_report_verify1_df.info())

